package com.epalmpay.controller.channel.datareport;

import com.epalmpay.commom.BaseController;
import com.epalmpay.entity.Agent;
import com.epalmpay.entity.Shop;
import com.epalmpay.exception.ApplicationException;
import com.epalmpay.service.yunstream.IObdService;
import com.epalmpay.util.DateUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.management.resources.agent;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by Administrator on 2018/1/12.
 */
@Controller
@RequestMapping(value="/merchant/obdReport2")
public class ObdReportController2 extends BaseController {

    @Resource
    private IObdService obdService;



    /**
     * 显示提现对账数据列表
     * @param
     * @return
     */
    @RequestMapping(value="/list")
    @ResponseBody
    public List list(){
        Map map = new HashMap();
        List<Agent> agentList=null;
        List<Shop> shopList=null;
        String agentIds="";
        String shopIds="";
        map.put("groupId",getRequest().getParameter("groupid"));
        map.put("searchkey",getRequest().getParameter("searchkey"));
        if(getRequest().getParameter("shopId").equals("-1")){
            map.put("isVaild",0);
        }
        if(getRequest().getParameter("agentId").equals("1")){
            int level =1;
            agentList= obdService.queryAgentIdsByGroupId(Long.parseLong(getRequest().getParameter("groupid")),level);
            map.put("status",1);
        }
        if(getRequest().getParameter("agentId").equals("2")){
            int level =1;
            agentList= obdService.queryAgentIdsByGroupId(Long.parseLong(getRequest().getParameter("groupid")),level);
            map.put("status",2);
        }
        if(getRequest().getParameter("agentId").equals("3")){
            int level =2;
            agentList= obdService.queryAgentIdsByGroupId(Long.parseLong(getRequest().getParameter("groupid")),level);
            map.put("status",1);
        }
        if(getRequest().getParameter("agentId").equals("4")){
            int level =2;
            agentList= obdService.queryAgentIdsByGroupId(Long.parseLong(getRequest().getParameter("groupid")),level);
            map.put("status",2);
        }
        if(getRequest().getParameter("agentId").equals("5")){
            int level =3;
            agentList= obdService.queryAgentIdsByGroupId(Long.parseLong(getRequest().getParameter("groupid")),level);
            map.put("status",1);
        }
        if(getRequest().getParameter("agentId").equals("6")){
            int level =3;
            agentList= obdService.queryAgentIdsByGroupId(Long.parseLong(getRequest().getParameter("groupid")),level);
            map.put("status",2);
        }
        if(agentList !=null && agentList.size()>0){
            for(Agent agent:agentList){
                agentIds +=agent.getId();
                agentIds +=',';
            }
            agentIds.substring(0,agentIds.length()-1);
            map.put("agentIds",agentIds);
        }

        if(getRequest().getParameter("shopId").equals("1")){
            shopList = obdService.queryShopIdByGroupId(Long.parseLong(getRequest().getParameter("groupid")));
            if(shopList !=null && shopList.size()>0){
                for(Shop shop:shopList){
                    shopIds +=shop.getId();
                    shopIds +=',';
                }
                shopIds.substring(0,shopIds.length());
                map.put("shopIds",shopIds);
            }
            map.put("status",1);
        }
        if(getRequest().getParameter("shopId").equals("2")){
            shopList = obdService.queryShopIdByGroupId(Long.parseLong(getRequest().getParameter("groupid")));
            if(shopList !=null &&shopList.size()>0){
                for(Shop shop:shopList){
                    shopIds +=shop.getId();
                    shopIds +=',';
                }
                shopIds.substring(0,shopIds.length());
                map.put("shopIds",shopIds);
            }
            map.put("status",2);
        }

        List result = new ArrayList();
        try {
//            result = obdService.getGroupMonthList(map);

            if(getRequest().getParameter("shopId").equals("1") ||getRequest().getParameter("agentId").equals("1")||
                    getRequest().getParameter("agentId").equals("3")||getRequest().getParameter("agentId").equals("5")){
                result = obdService.getGroupMonthList2(map);
            }else if(getRequest().getParameter("shopId").equals("-1")){
                result = obdService.getGroupMonthList3(map);
            }else{
                result = obdService.getGroupMonthList(map);
            }



        } catch (ApplicationException ae) {
            result = null;
            ae.printStackTrace();
        } catch (Exception e) {
            result = null;
            e.printStackTrace();
        }
        System.out.println(result);
        return result;
    }



    /**
     * 显示提现对账日明细数据列表
     * @param
     * @return
     */
    @RequestMapping(value="/daylist")
    @ResponseBody
    public List daylist(){
        Map map = new HashMap();
        map.put("groupId",getRequest().getParameter("groupid"));
        map.put("searchkey",getRequest().getParameter("searchkey"));
        List result = new ArrayList();
        List<Map<String, Object>> dlresult = null;
        try {
            dlresult = obdService.getObdDayList(map);
            //处理商户和代理商日明细
            for(Map m : dlresult) {
                Map rp = new HashMap();
                if(m.get("agentName") != null) {
                    rp.put("agentName", m.get("agentName").toString());
                }
                if( m.get("shopName") != null) {
                    rp.put("shopName", m.get("shopName").toString());
                }
                if( m.get("memberName")!=null){
                    rp.put("memberName", m.get("memberName").toString());
                }
                rp.put("deviceId", m.get("device_id").toString());
                if( m.get("sim_no") !=null){
                    rp.put("simNo", m.get("sim_no").toString());
                }
                rp.put("deviceVersion", m.get("device_version").toString());
                if( m.get("distribution_time") !=null){
                    rp.put("distributionTime", m.get("distribution_time").toString());
                }
                if(m.get("device_status") !=null){
                    rp.put("deviceStatus", m.get("device_status").toString());
                }
                rp.put("isProfit", m.get("is_profit"));
                if(m.get("activate_time") !=null){
                    rp.put("activateTime", m.get("activate_time").toString());
                }
                rp.put("gmtCreate", m.get("gmt_create").toString());
                rp.put("gmtModified", m.get("gmt_modified").toString());
                rp.put("isVaild", m.get("is_valid").toString());

                rp.put("datet", m.get("gmt_create").toString());
                result.add(rp);
            }


            //对结果按日期进行排序
            Collections.sort(result, new Comparator<Map>() {

                @Override
                public int compare(Map o1, Map o2) {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String date1 = o1.get("datet").toString();
                    String date2 = o2.get("datet").toString();

                    int i = 0;
                    try {
                        i = (int)(sdf.parse(date2).getTime() - sdf.parse(date1).getTime());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return i;
                }
            });

        } catch (ApplicationException ae) {
            result = null;
            ae.printStackTrace();
        } catch (Exception e) {
            result = null;
            e.printStackTrace();
        }
        System.out.println(result);
        return result;
    }

    /**
     * 显示提现对账月明细数据列表
     * @param
     * @return
     */
    @RequestMapping(value="/monthlist")
    @ResponseBody
    public List monthlist(){
        Map map = new HashMap();
        map.put("groupId",getRequest().getParameter("groupid"));
        map.put("searchkey",getRequest().getParameter("searchkey"));
        List result = new ArrayList();
        List<Map<String, Object>> memberresult = null;
        List<Map<String, Object>> dlresult = null;
        try {
            dlresult = obdService.getObdMonthList(map);
            //处理商户和代理商日明细
            for(Map m : dlresult) {
                Map rp = new HashMap();
                if(m.get("agentName") != null) {
                    rp.put("agentName", m.get("agentName").toString());
                }
                if( m.get("shopName") != null) {
                    rp.put("shopName", m.get("shopName").toString());
                }
                if( m.get("memberName")!=null){
                    rp.put("memberName", m.get("memberName").toString());
                }
                rp.put("deviceId", m.get("device_id").toString());
                if( m.get("sim_no") !=null){
                    rp.put("simNo", m.get("sim_no").toString());
                }
                rp.put("deviceVersion", m.get("device_version").toString());
                if( m.get("distribution_time") !=null){
                    rp.put("distributionTime", m.get("distribution_time").toString());
                }
                if(m.get("device_status") !=null){
                    rp.put("deviceStatus", m.get("device_status").toString());
                }
                rp.put("isProfit", m.get("is_profit"));
                if(m.get("activate_time") !=null){
                    rp.put("activateTime", m.get("activate_time").toString());
                }
                rp.put("gmtCreate", m.get("gmt_create").toString());
                rp.put("gmtModified", m.get("gmt_modified").toString());
                rp.put("isVaild", m.get("is_valid").toString());

                rp.put("datet", m.get("gmt_create").toString());
                result.add(rp);
            }


            //对结果按日期进行排序
            Collections.sort(result, new Comparator<Map>() {

                @Override
                public int compare(Map o1, Map o2) {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String date1 = o1.get("datet").toString();
                    String date2 = o2.get("datet").toString();

                    int i = 0;
                    try {
                        i = (int)(sdf.parse(date2).getTime() - sdf.parse(date1).getTime());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return i;
                }
            });

        } catch (ApplicationException ae) {
            result = null;
            ae.printStackTrace();
        } catch (Exception e) {
            result = null;
            e.printStackTrace();
        }
        System.out.println(result);
        return result;
    }


}
